
// 手写promise
function myPromise (executor){
    this.status = 'pending';
    this.result = undefined;
    this.reason = undefined;
    const self = this;
    function resolve(result){
        if(self.status === 'pending'){
            self.status = 'resolved';
            self.result = result;
        } 
    }
    function reject(reson){
        if(self.status === 'pending'){
            self.status = 'rejected';
            self.reason = reson
        } 
    }
    executor(resolve,reject)// 同步执行
}

myPromise.prototype.then=function(onResolved, onRejected){
    const self = this;
    if(self.status === 'resolved'){
        onResolved(self.result)
    }

    if(self.status === 'rejected'){
        onRejected(self.reason)
    }
}

myPromise.prototype.catch=function(onRejected){
    const self = this;
    if(self.status === 'rejected'){
        onRejected(self.reason)
    }
}

let tryPromise = new myPromise((resolve,reject)=>{
    console.log('成功aaa')
    resolve('成功')

});

tryPromise.then((res)=>{
    console.log('成功了',res)
})

console.log('22222')